package ku.report.dao;

import java.sql.Date;
import java.sql.SQLException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ku.report.dto.EmployeesDTO;
import ku.report.dto.WeekReportDTO;
import ku.report.ibatis.SqlDaoSuport;
import ku.report.idao.IManagerWeekReportDao;


public class ManagerWeekReportDao extends SqlDaoSuport implements  IManagerWeekReportDao{

		public ManagerWeekReportDao(String pass) {
			super(pass);
		}//
		public ManagerWeekReportDao() {
			super();
		}//
		
		@SuppressWarnings("unchecked")
		public List<WeekReportDTO> getCustUserList(int startpage, int endpage ,String app,String typenumber){
			Map<String, Object> params = new HashMap<String, Object>();
			System.out.println("--------getCustUser	List------------");
			System.out.println("startpage= "+startpage+"\t endpage="+endpage);
			
			System.out.println("typenumber="+typenumber);
			params.put("startpage", new Integer(startpage));
			params.put("endpage", new Integer(endpage));
			params.put("app", app);
			params.put("typenumber", typenumber);
			List<WeekReportDTO> result=null;
			try {
				if(app.equals("")){
				result=(List<WeekReportDTO>)this.getSqlMapper().queryForList("getManagerWeekReportList2",params);
				System.out.println("app="+app);
				System.out.println("result="+result);
				}else{
				result=(List<WeekReportDTO>)this.getSqlMapper().queryForList("getManagerWeekReportList",params);
			}
			}catch (Exception e) {
	 
				System.out.println(e);
			}		
			
			return result;
		}//
		public int getCount(String app, String typenumber) {
			int answer=-1;
			System.out.println("--------getCount------------");
			System.out.println("--------------------");
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("app", app);
			params.put("typenumber", typenumber);
			
			try {
				if(app.equals("")){
					answer = (Integer)this.getSqlMapper().queryForObject("getCount2",params);
				}else{
				answer = (Integer)this.getSqlMapper().queryForObject("getCount",params);
			}
			}catch (Exception e) {
				System.out.println(e);
			}
			return answer; 
		}//
		public WeekReportDTO getWeekReport(String report_seq) {
			
			System.out.println("--------getCustUser------------");
			System.out.println("report_seq= "+report_seq);
			System.out.println("-------------------------------");
			
			WeekReportDTO result=null;
			try {
				result=(WeekReportDTO)this.getSqlMapper().queryForObject("getWeekReport",report_seq);
			} catch (Exception e) {

				System.out.println(e);
			}		 
			
			return result;
		}//
		public int addCustUser(WeekReportDTO uDto){
			int answer=1;
			try {
				System.out.println("--------addCustUser------------");
				System.out.println("-------------------------------");
				this.getSqlMapper().insert("addCustUser", uDto);
			} catch (Exception e) {
				System.out.println(e);
				answer=-1;
			}
			return answer;
		}//
		public int updateWeekReport(WeekReportDTO uDto){
			int count=1;
			System.out.println(uDto);
			try {
				if(uDto.getPmcheck().equals("1")){
					this.getSqlMapper().update("pmCheck", uDto);
				}else {
				this.getSqlMapper().update("updateWeekReport", uDto);
				}
			} catch (SQLException e) {
				System.out.println(e);
				count=-1;
			}
			return count;
		}//
		public int deleteCustUser(String id){
			int count=1;
			try{
				this.getSqlMapper().delete("deleteCustUser",id);
			}catch (Exception e) {
				count=-1;
				System.out.println(e);
			}
			return count;
		}//
		public boolean deleteCustUsers(String[] ids){
			boolean isb=true;
			try{
				this.getSqlMapper().startTransaction();
				this.getSqlMapper().startBatch();
				for (int i = 0; i < ids.length; i++) {
					this.getSqlMapper().delete("deleteCustUser",ids[i]);
				}					
				this.getSqlMapper().executeBatch();
				this.getSqlMapper().commitTransaction();
			}catch (Exception e) {
				isb=false;
				System.out.println(e);
			}
			finally{
				try {
					this.getSqlMapper().endTransaction();
				} catch (SQLException e) {
					
				}
			}
			return isb;
		}//
		@Override
		public List<EmployeesDTO> getEmploList() {
			 List<EmployeesDTO> emplist =null;
			
				System.out.println("--------getEmploList------------");
			

				try {
					emplist=(List<EmployeesDTO>)this.getSqlMapper().queryForList("empList");
				} catch (Exception e) {

					System.out.println(e);
				}		 
				
			return emplist;
		}
		public List<WeekReportDTO> getCustUserSearchList(int startn, int endn, 
				String serchType,String serchValue,String typenumber){
			List<WeekReportDTO> lists = null;
			Map<String, Object> params = new HashMap<String, Object>();
			System.out.println("typenumber="+typenumber);
			params.put(serchType, "%"+serchValue+"%");	
			params.put("startpage", new Integer(startn));
			params.put("endpage", new Integer(endn));
			params.put("typenumber",typenumber.trim());	
			
			try {
				lists = (List<WeekReportDTO>)this.getSqlMapper().queryForList("search", params);
				System.out.println("paging : lists = "+lists);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return lists; 
			
		}
		public int getCustUserSearchCount(String serchType,String serchValue,String typenumber) {
			
			int count = 0;
			Map<String, Object> params = new HashMap<String, Object>();
			
			if(serchType!=null){
					if(serchValue!=null && serchValue.length()>0){
						params.put(serchType, serchValue);
						params.put("typenumber",typenumber.trim());	
					}
				}
			try {
				
				count = (Integer)this.getSqlMapper().queryForObject("searchcount", params);
			} catch (SQLException e) {
				e.printStackTrace();
			
		}//
		return count;
	}//
		@Override
		public List<WeekReportDTO> getAllMonthReport(String weeknum) {
			List<WeekReportDTO> wdto = null;
			try {
				wdto = (List<WeekReportDTO>)this.getSqlMapper().queryForList("getAllWeekReport", weeknum);
			} catch (SQLException e) {
				System.out.println("WeekReportDAO : getAllWeekReport === " + e);
			}
			return wdto;			
		}
	}


